<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">客户管理</div></el-breadcrumb-item
        >
        <el-breadcrumb-item :to="{ path: '/customerIntention' }"
          ><div class="div2">客户意向设定</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <!-- 添加客户等级 -->
    <div class="content" v-if="parArray.indexOf('yxAdd') != -1">
      <p class="header">管理客户意向</p>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="意向名称">
          <el-input
            maxlength="128"
            v-model="formInline.name"
            placeholder="请输入客户意向名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门选择">
          <el-select
            v-model="formInline.team"
            multiple
            placeholder="选择部门"
            clearable
          >
            <el-option
              v-for="itemsS in logisticsCont"
              :key="itemsS.dictId"
              :label="itemsS.dictName"
              :value="itemsS.dictId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            v-if="parArray.indexOf('yxAdd') != -1"
            >添加意向</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加客户等级 -->
    <!-- 管理客户等级 -->
    <div class="manage">
      <p class="header">管理客户意向</p>
      <div class="table">
        <el-table :data="tableData.list">
          <el-table-column
            type="index"
            label="序号"
            width="200"
          ></el-table-column>
          <el-table-column scope="scope" label="意向名称" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="部门权限" width="400">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.salesAuthorityIds"
                multiple
                placeholder="选择部门"
                clearable
              >
                <el-option
                  v-for="itemsS in logisticsCont"
                  :key="itemsS.dictId"
                  :label="itemsS.dictName"
                  :value="itemsS.dictId"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            prop="createdTime"
            label="创建时间"
          ></el-table-column>
          <el-table-column label="操作" width="300">
            <template slot-scope="scope">
              <div class="optain">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="保存"
                  placement="top-start"
                >
                  <p
                    @click="preservation(scope.row)"
                    v-if="parArray.indexOf('yxbc') != -1"
                  >
																		<span>保存</span>
                    <!-- <img src="../../assets/baocun.png" alt="" /> -->
                  </p>
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="置顶"
                  placement="top-start"
                >
                  <p
                    @click="topData(scope.row)"
                    v-if="parArray.indexOf('yxzd') != -1"
                  >
																		<span>置顶</span>
                    <!-- <img src="../../assets/top.png" style="width:16px" alt="" /> -->
                  </p>
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="向下移动"
                  placement="top-start"
                >
                  <p v-if="parArray.indexOf('yxbottom') != -1">
                    <!-- <img
                      src="../../assets/down.png"
                      style="width:16px"
                      alt=""
                    /> -->
																					<span>下移</span>
                  </p>
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="向上移动"
                  placement="top-start"
                >
                  <p v-if="parArray.indexOf('yxtop') != -1">
																				<span>上移</span>
                    <!-- <img src="../../assets/up.png" style="width:16px" alt="" /> -->
                  </p>
                </el-tooltip>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="top-start"
                >
                  <p
                    @click="delect(scope.row, scope.$index)"
                    v-if="parArray.indexOf('yxdelect') != -1"
                  >
																		<span>删除</span>
                    <!-- <img src="../../assets/delect.png" alt="" /> -->
                  </p>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 管理客户等级 -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      formInline: {
        name: '',
        team: ''
      },
      tableData: [],
      limits: '',
      parArray: [],
      logisticsCont: []
    }
  },
  created () {
    this.listAxios()
    this.groupEvent()
    // 权限设定判断 normsCode
    this.limits = JSON.parse(sessionStorage.getItem('limit'))
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === 'customerIntentionCode'
    )
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code)
    })
    // 权限设定判断 normsCode
  },
  activated () {
    // this.listAxios();
    // this.groupEvent();
  },
  methods: {
    //* **列表***//
    listAxios () {
      this.axios
        .post('/order/comm/customerintention/list', {
          pageNo: 1,
          pageSize: 20,
          salesAuthorityId: JSON.parse(
            sessionStorage.getItem('salesAuthorityId')
          )
        })
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
    },
    //* ** 添加意向设定 ***//
    onSubmit () {
      if (this.formInline.name == '') {
        this.$message.error('名称不能为空')
      } else if (this.formInline.team == '') {
        this.$message.error('部门不能为空')
      } else {
        this.axios
          .post(
            '/order/comm/customerintention/add',
            {
              createdBy: sessionStorage.getItem('user_id'),
              name: this.formInline.name,
              salesAuthorityIds: this.formInline.team
            },
            {
              headers: {
                isloading: false
              }
            }
          )
          .then((res) => {
            // console.log(res);
            if (res.code == 0) {
              this.$message.success('添加成功')
              this.formInline.name = ''
              this.formInline.team = ''
              this.listAxios()
            } else {
              this.$message.error(res.message)
            }
          })
      }
    },
    // 部门下拉框
    groupEvent () {
      this.axios
        .get('/sys/v1/common/by/group?groupId=' + 'sales_authority', {
          headers: {
            isLoading: false
          }
        })
        .then((res) => {
          if (res.code == 0) {
            this.logisticsCont = res.data
          } else {
            this.$message.error(res.message)
          }
        })
    },
    //* ** 添加意向设定 ***//
    /** *删除数据对接***/
    delect (row, index) {
      this.$confirm('确定要删除该意向吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.axios
            .get('/order/comm/customerintention/delete?id=' + row.id)
            .then((res) => {
              if (res.code == 0) {
                this.tableData.list.splice(index, 1)
                this.listAxios('1')
                this.$message.success('删除成功')
              } else {
                this.$message.error(res.message)
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /** *删除数据对接***/

    /** *修改数据对接***/
    preservation (row) {
      this.axios
        .post('/order/comm/customerintention/update', {
          id: row.id,
          updatedBy: sessionStorage.getItem('user_id'),
          name: row.name,
          salesAuthorityIds: row.salesAuthorityIds
        })
        .then((res) => {
          this.listAxios('1')
          if (res.code == 0) {
            this.$message.success('修改成功')
          } else {
            this.$message.error(res.message)
          }
        })
    },
    /** *修改数据对接***/

    /** *顶置数据对接***/
    topData (row) {
      this.axios
        .get('/order/comm/customerintention/isTop?id=' + row.id)
        .then((res) => {
          this.listAxios('1')
          if (res.code == 0) {
            this.$message.success('顶置成功')
          } else {
            this.$message.error(res.message)
          }
        })
    }
    /** *顶置数据对接***/
  }
}
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.content {
  padding: 10px;
  background: #fff;
  .el-button {
    padding: 6px 10px;
  }
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
/deep/.el-input__icon {
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
/deep/.el-table__row .cell {
}
.optain {
  width: 100%;
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  p {
    display: inline-block;
    margin-left: 20px;
    img {
      width: 20px;
      height: 20px;
    }
  }
}
</style>
